import { constantRoutes } from '@/router'

// 按需导入所有的动态路由
import { asyncRoutes } from '@/router'

const state = {
  // vue router他没有帮我们维护一个新增后的路由规则数组, 所有我们自己维护一个数组(静态+动态)
  routes: constantRoutes || []
}

const mutations = {
  setRoutes(state, otherRoutes) {
    state.routes = [...constantRoutes, ...otherRoutes]
    // state.routes.push()
  }
}

// 封装异步操作(同步操作..)
const actions = {
  // 过滤出用户自己的路由规则 asyncRoutes menus
  filterRoutes(context, menus) {
    // console.log(asyncRoutes, menus, 6677)
    // item.children[0].name 判断的标准   (item.path 前面有/ 而且/:id)
    // item.children[0].name 如果在menus中出现, 要保留
    const otherRoutes = asyncRoutes.filter(item => menus.includes(item.children[0].name))
    context.commit('setRoutes', otherRoutes)
    console.log(asyncRoutes, menus, otherRoutes, 3344)

    return otherRoutes
  }
}
const getters = {}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
